package com.econ.powercloud.ems.service.dao.clickhouse;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.econ.powercloud.clickhouse.entity.EmsStatisticalMonth;
import com.econ.powercloud.clickhouse.mapper.EmsStatisticalMonthMapper;
import com.econ.powercloud.ems.pojo.requestVO.ReportRequestVO;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Objects;

/**
 * @author jwc
 * @date 2022/10/31
 */
@Repository
@DS("clickhouse")
public class EmsStatisticalMonthDAO extends ServiceImpl<EmsStatisticalMonthMapper, EmsStatisticalMonth> {

    public List<EmsStatisticalMonth> selectByCondition(ReportRequestVO requestVO) {
        requestVO.setEquipmentId(Objects.isNull(requestVO.getEquipmentId()) ? StringUtils.EMPTY :requestVO.getEquipmentId());
        LambdaQueryWrapper<EmsStatisticalMonth> wrapper = Wrappers.<EmsStatisticalMonth>lambdaQuery()
                .le(EmsStatisticalMonth::getTime, requestVO.getEndTime())
                .ge(EmsStatisticalMonth::getTime, requestVO.getStartTime())
                .eq(StringUtils.isNotEmpty(requestVO.getStationId()), EmsStatisticalMonth::getEnergyStationId, requestVO.getStationId())
                .eq(StringUtils.isNotEmpty(requestVO.getEquipmentId()),EmsStatisticalMonth::getEnergyEquipmentId, requestVO.getEquipmentId())
                .eq(StringUtils.isNotEmpty(requestVO.getReportId()), EmsStatisticalMonth::getReportId,requestVO.getReportId())
                .eq(StringUtils.isNotEmpty(requestVO.getDeviceCode()), EmsStatisticalMonth::getDeviceCode,requestVO.getDeviceCode())
                .in(CollectionUtils.isNotEmpty(requestVO.getColumnIdList()), EmsStatisticalMonth::getColumnId,requestVO.getColumnIdList());
        return this.list(wrapper);
    }

    public List<EmsStatisticalMonth> selectByCondition(Long startMonthTime, Long startDayTime, String reportId) {
        LambdaQueryWrapper<EmsStatisticalMonth> wrapper = Wrappers.<EmsStatisticalMonth>lambdaQuery()
                .le(EmsStatisticalMonth::getTime, startDayTime)
                .ge(EmsStatisticalMonth::getTime, startMonthTime)
                .eq(EmsStatisticalMonth::getReportId, reportId)
                .ne(EmsStatisticalMonth::getDeviceCode, StringUtils.EMPTY);
        return this.list(wrapper);
    }

    public List<EmsStatisticalMonth> selectListByStartTimeAndEndTimeAndReportId(Long startTime, Long endTime, String reportId){
        LambdaQueryWrapper<EmsStatisticalMonth> wrapper = Wrappers.<EmsStatisticalMonth>lambdaQuery()
                .le(EmsStatisticalMonth::getTime, endTime)
                .ge(EmsStatisticalMonth::getTime, startTime)
                .eq(EmsStatisticalMonth::getReportId, reportId);
        return this.list(wrapper);
    }

    public void insertBatch(List<EmsStatisticalMonth> list){
        this.getBaseMapper().insertBatch(list);
    }

    public void deleteByReportIdAndTime(String reportId, Long time) {
        this.getBaseMapper().deleteByReportIdAndTime(reportId, time);
    }
}
